package com.xszx.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xszx.commonutils.JwtUtils;
import com.xszx.commonutils.R;
import com.xszx.order.entity.Order;
import com.xszx.order.service.OrderService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author 白熊
 * @since 2024-07-26
 */
@RestController
@RequestMapping("/order")
//@CrossOrigin
@Api(tags = "订单管理")
public class OrderController {
    @Autowired
    private OrderService orderService;


    //根据用户id和课程id来确认该用户是否购买该课程
    @GetMapping("isBuyCourse/{memberId}/{courseId}")
    public boolean isBuyCourse(@PathVariable("memberId") String memberId,
                               @PathVariable("courseId") String courseId) {
        //订单状态是1表示支付成功
        int count = orderService.count(new QueryWrapper<Order>()
                .eq("member_id", memberId)
                .eq("course_id", courseId)
                .eq("status", 1));

        if(count>0) {
            return true;
        } else {
            return false;
        }
    }


    //根据课程id和用户id创建订单，返回订单id
    @PostMapping("createOrder/{courseId}")
    public R save(@PathVariable("courseId") String courseId, HttpServletRequest request) {
        String orderNo = orderService.saveOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderNo);
    }

    //获取订单
    @GetMapping("getOrder/{orderId}")
    public R get(@PathVariable String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_no",orderId);
        //获取单条数据
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }

}

